package co.edu.unal.bioingenium.kbmed.web.servlets;

import co.edu.unal.bioingenium.kbmed.config.Configuration;
import java.io.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Luis A Riveros
 */
public class DocumentServlet extends HttpServlet {

    /**
     *
     */
    @Override
    public void init() {
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String docId = (String) request.getParameter("docId");
        // Get the absolute path of the image
        ServletContext servletContext = getServletContext();
        String filePath = Configuration.COLLECTION_PATH + docId;
        // Get the MIME type of the image
        String mimeType = servletContext.getMimeType(filePath);
        if (mimeType == null) {
            servletContext.log("Could not get MIME type of " + filePath);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
        // Set content type
        response.setContentType(mimeType);
        response.setCharacterEncoding("UTF-8");
        // Set content size
        File file = new File(filePath);
        response.setContentLength((int) file.length());
        // Open the file and output streams
        FileInputStream in = new FileInputStream(file);
        OutputStream out = response.getOutputStream();
        // Copy the contents of the file to the output stream
        byte[] buffer = new byte[1024];
        int count = 0;
        while ((count = in.read(buffer)) >= 0) {
            out.write(buffer, 0, count);
        }
        in.close();
        out.close();

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Document Servlet";
    }
}
